//
//  WelComeViewController.swift
//  SinaWeibo
//
//  Created by 林柱强 on 16/2/2.
//  Copyright © 2016年 林柱强. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage

class WelComeViewController: UIViewController {
    
    /// 记录底部约束
    var bottomCons: NSLayoutConstraint?
    override func viewDidLoad() {
        super.viewDidLoad()
        setupUI()
        // Do any additional setup after loading the view.
    }

    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        startAnimation()
    }
    
    private func startAnimation() {
        //usingSpringWithDamping  弹簧系数  0~1之间  越小越弹
        //initialSpringVelocity 加速度
        //弹簧效果 和加速度之间的关系: 弹簧效果 * 10 = 加速度
        
        //外部改变约束
        bottomCons?.constant = -UIScreen.mainScreen().bounds.height -  bottomCons!.constant
        
        /*
        一般在自动布局中使用动画效果  不建议直接修改
        自动布局系统 会根据设置的约束 在layoutSubViews方法中自动修改frame
        frame 是有自动布局系统来控制的
        如果希望能够提前刷新修改的约束效果  需要调用 layoutIfNeeded() 让约束提前刷新 ,更新frame
        */
        
        //动画效果 由远到近 涉及到加速度
        UIView.animateWithDuration(1.2, delay: 0, usingSpringWithDamping: 0.9, initialSpringVelocity: 9.8, options: [], animations: { () -> Void in
            //强制刷新页面 将提前收集到的约束的变化 进行更新
            self.view.layoutIfNeeded()
            }) { (_) -> Void in
                UIView.animateWithDuration(0.6, animations: { () -> Void in
                    self.welcomeLabel.alpha = 1
                    }, completion: {(_) -> Void in
                        //发出通知
                        NSNotificationCenter.defaultCenter().postNotificationName(APPSwitchRootViewController, object: nil)
                })
        }
    }

    private func setupUI() {
        //1.添加控件
        view.addSubview(backImage)
        view.addSubview(iconImage)
        view.addSubview(welcomeLabel)
        
        //2.设置布局
        backImage.xmg_Fill(view)
        
        let cons = iconImage.xmg_AlignInner(type: XMG_AlignType.BottomCenter, referView: view, size: CGSize(width: 100, height: 100), offset: CGPoint(x: 0, y: -150))
        // 拿到头像的底部约束
        bottomCons = iconImage.xmg_Constraint(cons, attribute: NSLayoutAttribute.Bottom)
        
        welcomeLabel.xmg_AlignVertical(type: XMG_AlignType.BottomCenter, referView: iconImage, size: nil, offset: CGPoint(x: 0, y: 20))
        
        //加载图片
        if let iconUrl = UserAccount.loadAccount()?.avatar_large
        {
            let url = NSURL(string: iconUrl)!
            iconImage.sd_setImageWithURL(url)
        }
        
        //给图片设置成圆角
        iconImage.layer.cornerRadius = 45
        iconImage.layer.masksToBounds = true
    }
    
    //懒加载
    private lazy var backImage: UIImageView = UIImageView(image: UIImage(named: "ad_background"))
    
    private lazy var iconImage: UIImageView = {
        let iv = UIImageView(image: UIImage(named: "avatar_default_big"))
        iv.layer.cornerRadius = 50
        iv.clipsToBounds = true
        return iv
        }()

    
    private lazy var welcomeLabel: UILabel = {
        let label = UILabel()
        
        label.textColor = UIColor.lightGrayColor()
        label.font = UIFont.systemFontOfSize(19)
        label.textAlignment = .Center
        label.text = ("") + "欢迎回来"
        label.alpha = 0
        label.sizeToFit()
        return label
    }()
}
